angular.module('Controller')

.controller('QgyApartmentManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            apartmentId:$localStorage['accountStore']
            city:undefined
            name:undefined
            enabled:true
        }
    $scope.storeAu=$localStorage['accountStore']
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        requestData={
            page: params.page() - 1,
            size: params.count(),
            apartmentId:$scope.search.apartmentId
            name: $scope.search.name,
            city: $scope.search.city,
            enabled: $scope.search.enabled,
        }
        $http.get('/apartment',params:requestData)
        .success (resp)->
            if resp.code==0
                list=resp.message.content
                $scope.tableData=list
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:false}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            $http.get("/apartment/#{item.id}/detail")
            .success((resp)->
                if resp.code==0
                    theData=_.extend(theData,resp.message)
                    showModal('edit',theData)
            )
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        configPicture:(item)->
            $rootScope.pageParams=angular.copy(item)
            $rootScope.pageType='edit'
            menuManage.goState('apartment-picture',true)
        configUnit:(item)->
            $rootScope.pageParams={apartmentId:item.id,apartmentName:item.name,uuid:item.uuid}
            $rootScope.pageType='configUnit'
            menuManage.goState('apartment-dynamic-config',true)
        configLayout:(item)->
            $rootScope.pageParams={apartmentId:item.id,apartmentName:item.name,uuid:item.uuid}
            $rootScope.pageType='configLayout'
            menuManage.goState('apartment-layout-manage',true)
        editConfigure:(item)->
            $rootScope.pageParams={apartmentId:item.id,apartmentName:item.name,uuid:item.uuid}
            $rootScope.pageType='configure-type'
            menuManage.goState('apartment-configure-type',true)
        goHouseList:(item)->
            $rootScope.pageParams={apartmentId:item.id,apartmentName:item.name,uuid:item.uuid}
            $rootScope.pageType='room-manage'
            menuManage.goState('room-manage',true)
        removeData:(item)->
            $http.delete('/apartment/'+item.id)
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('公寓删除成功')
                    $scope.queryData()
                else
                    wfAlert.toastError(resp.message)
            )
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            size:'lg',
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyApartmentEditCtrl',
            templateUrl: 'templates/project-manage/apartment-manage/apartment-manage-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)